home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: 76623,2065@compuserve.com (Bobby Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: Operator Overloading
- Date: 9 Apr 1996 13:01:47 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4kdn3r$rb9@arl-news-svc-2.compuserve.com>
- References: <9604071905.AA001o6@lorelei.demon.co.uk>
- Reply-To: 76623,2065@compuserve.com (Bobby Martin)
- NNTP-Posting-Host: hd23-013.compuserve.com
- X-Newsreader: IBM NewsReader/2 v1.03
-
- You have in fact found user-defined casting operators. They allow you to
- define ways in which your class can be *automatically* converted into the
- type you specify. The definition in the streams library allows the stream to
- be automatically converted to a void pointer, and the one you defined in
- your class allows it to be automatically converted into a double.
-
- Note that these functions will be called whenever the compiler determines that
- the type casted to is required and the class in which the cast is defined is
- given. There is no warning that this function is called. For example, if you
- define a String class that has an operator const char*() const defined, the
- following will compile:
-
- String name("Bobby Martin");
-
- strcmp( name, "John Hancock");
-
- This can be very useful in some cases, but can lead to code that's confusing
- to the user on second glance.
-
- Hope that helps,
-
- Bobby Martin
-
- In <9604071905.AA001o6@lorelei.demon.co.uk>, John Croudy <john@lorelei.demon.co.uk> writes:
- >Hello,
- >
- >I have a question which I can't find the answer to in any of my books.
- ..
- <snip>
- ..
- > operator void*() const { return something; }
- >
- ..
- <snip>
- ..
- >It seems to be a user-defined casting operator, but I can't find any
- >mention of this in any books I have. Can anyone explain what I have
- >discovered here?
- >
- >Thanks,
- >
- >John
- >xxxx
-
-